package com.xqls.service.impl;

import com.xqls.bean.CompanyBean;
import com.xqls.dal.dao.CompanyDao;
import com.xqls.dal.model.Company;
import com.xqls.dal.model.CompanyExample;
import com.xqls.service.CompanyService;
import com.xqls.util.CommonResult;
import com.xqls.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl implements CompanyService{

	@Autowired private CompanyDao dao;
	
	@Override
	public boolean add(CompanyBean bean){
		return dao.insert((Company)ObjectUtil.transfer(bean, Company.class));
	}
	
	@Override
	public boolean update(CompanyBean bean){
		return dao.update((Company)ObjectUtil.transfer(bean, Company.class));
	}
	
	@Override
	public boolean deleteByBlockId(Integer blockId){
		return dao.deleteByBlockId(blockId);
	}
	
	@Override
	public boolean deleteList(List<Integer> keys){
		return dao.deleteList(keys);
	}
	
	@Override
	public CompanyBean queryByKey(Integer key){
		return (CompanyBean) ObjectUtil.transfer(dao.queryByKey(key), CompanyBean.class);
	}

	@Override
	public List<String> getList(List<Integer> keys) {
		CompanyExample example = new CompanyExample();
		example.createCriteria().andCompanyIdIn(keys);
		List<Company> companys=dao.queryByExample(example);
		if(companys!=null&&companys.size()>0){
			List<String>  deviceNos=new ArrayList<>();
			for (Company company:companys){
				deviceNos.add(company.getDeviceNo());
			}
			return  deviceNos;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public CommonResult queryByDeviceNo(String deviceNo){
		CommonResult commonResult=new  CommonResult();
		CompanyExample example = new CompanyExample();
		example.createCriteria().andDeviceNoEqualTo(deviceNo);
		List<CompanyBean> companyBeens= ObjectUtil.transferList(dao.queryByExample(example), CompanyBean.class);
		if(companyBeens!=null&&companyBeens.size()>0){
			commonResult.setIsSuccess(true);
			commonResult.setData(companyBeens.get(0));
		}else if(companyBeens==null||companyBeens.size()==0){
			commonResult.setIsSuccess(false);
			commonResult.setResultMsg("设备编号查询为空");
		}

		return commonResult;
	}
	
	@Override
	public List<Map<String, Object>> queryByMap(Map<String, Object> map){
		return dao.queryByBlockOrType(map);
	}

	@Override
	public List<Company> getList(Integer deviceType) {
		CompanyExample example = new CompanyExample();
		example.createCriteria().andDeviceTypeEqualTo(deviceType);
		return this.dao.queryByExample(example);
	}
	
	@Override
	public List<Company> getList() {
		CompanyExample example = new CompanyExample();
		return this.dao.queryByExample(example);
	}






}
